if (SERVER) then
	AddCSLuaFile("shared.lua")
	SWEP.HoldType			= "shotgun"
end

if (CLIENT) then		
	SWEP.Author				= "HLTV Proxy"
	SWEP.Slot				= 4
	SWEP.SlotPos			= 5
	SWEP.ViewModelFOV		= 50
	SWEP.ViewModelFlip		= false
	SWEP.IconLetter			= "7"
	SWEP.IconFont			= "HL2MPSelectIcons"
	SWEP.Category			= "Other"
	killicon.AddFont("shot_glround", "HL2MPTypeDeath", "7", Color(100, 100, 100, 255))
end

SWEP.PrintName			= "Grenade Launcher"	
SWEP.Base				= "weapon_mad_base_shotgun"

SWEP.Spawnable			= false
SWEP.AdminSpawnable		= true
SWEP.Upgraded			= false


SWEP.ViewModel			= "models/weapons/v_shotgun.mdl"
SWEP.WorldModel			= "models/weapons/w_shotgun.mdl"

SWEP.Weight				= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false

SWEP.Primary.Sound 		= Sound("NPC_Combine.GrenadeLaunch")
SWEP.Primary.Reload		= Sound("Weapon_SHOTGUN.Reload")
SWEP.Primary.Recoil			= 25
SWEP.Primary.Damage			= 40
SWEP.Primary.NumShots		= 1
SWEP.Primary.Cone			= 0.040
SWEP.Primary.ClipSize		= 3
SWEP.Primary.Delay			= 0.35
SWEP.Primary.DefaultClip	= 3
SWEP.Primary.Automatic		= false
SWEP.Primary.Ammo			= "SMG1_Grenade"

SWEP.IronSightsPos 		= Vector(6.1, -14, 2.5)
SWEP.IronSightsAng 		= Vector(2.8, 0, 0)

SWEP.Secondary.ClipSize		= -1					// Size of a clip
SWEP.Secondary.DefaultClip	= -1					// Default number of bullets in a clip
SWEP.Secondary.Automatic	= false				// Automatic/Semi Auto
SWEP.Secondary.Ammo		= "none"

SWEP.ShellEffect			= "effect_mad_shell_shotgun"	// "effect_mad_shell_pistol" or "effect_mad_shell_rifle" or "effect_mad_shell_shotgun"
SWEP.ShellDelay			= 0

SWEP.Pistol				= false
SWEP.Rifle				= false
SWEP.Shotgun			= true
SWEP.Sniper				= false

/*---------------------------------------------------------
   Name: SWEP:Precache()
   Desc: Use this function to precache stuff.
---------------------------------------------------------*/
function SWEP:Precache()

    	util.PrecacheSound("weapons/grenade_launcher1.wav")
    	util.PrecacheSound("weapons/shotgun/shotgun_reload1.wav")
    	util.PrecacheSound("weapons/shotgun/shotgun_reload2.wav")
    	util.PrecacheSound("weapons/shotgun/shotgun_reload3.wav")
end

/*---------------------------------------------------------
   Name: SWEP:Think()
   Desc: Called every frame.
---------------------------------------------------------*/
function SWEP:Think()

	if self.Weapon:Clip1() > self.Primary.ClipSize then
		self.Weapon:SetClip1(self.Primary.ClipSize)
	end

	if self.Weapon:GetNetworkedBool("Reloading") == true then
		if self.Weapon:GetNetworkedInt("ReloadTime") < CurTime() then
			if self.unavailable then return end

			if (self.Weapon:Clip1() >= self.Primary.ClipSize or self.Owner:GetAmmoCount(self.Primary.Ammo) <= 0) then
				self.Weapon:SetNextPrimaryFire(CurTime() + 1)
				self.Weapon:SetNextSecondaryFire(CurTime() + 1)
				self.Weapon:SetNetworkedInt("ReloadTime", CurTime() + 1)
				self.Weapon:SetNetworkedBool("Reloading", false)
				self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)

				if (IsValid(self.Owner) and self.Owner:GetViewModel()) then
					self:IdleAnimation(self.Owner:GetViewModel():SequenceDuration())
				end
			else
				self.Weapon:SetNetworkedInt("ReloadTime", CurTime() + 0.5)
				self.Weapon:SendWeaponAnim(ACT_VM_RELOAD)
				self.Owner:RemoveAmmo(1, self.Primary.Ammo, false)
				self.Weapon:SetClip1(self.Weapon:Clip1() + 1)
				self.Weapon:SetNextPrimaryFire(CurTime() + 1)
				self.Weapon:SetNextSecondaryFire(CurTime() + 1)
				self.Weapon:EmitSound(self.Primary.Reload)

				if (self.Weapon:Clip1() >= self.Primary.ClipSize or self.Owner:GetAmmoCount(self.Primary.Ammo) <= 0) then
					self.Weapon:SetNextPrimaryFire(CurTime() + 1)
					self.Weapon:SetNextSecondaryFire(CurTime() + 1)
				else
					self.Weapon:SetNextPrimaryFire(CurTime() + 1)
					self.Weapon:SetNextSecondaryFire(CurTime() + 1)
				end
			end
		end
	end

	if self.Owner:KeyPressed(IN_ATTACK) and (self.Weapon:GetNWBool("Reloading", true)) then
		self.Weapon:SetNextPrimaryFire(CurTime() + 0.5)
		self.Weapon:SetNextSecondaryFire(CurTime() + 0.5)
		self.Weapon:SetNetworkedInt("ReloadTime", CurTime() + 1)
		self.Weapon:SetNetworkedBool("Reloading", false)

		timer.Simple(self.Owner:GetViewModel():SequenceDuration(), function()
			if not self.Owner then return end
			self.Weapon:SendWeaponAnim(ACT_SHOTGUN_RELOAD_FINISH)

			if (IsValid(self.Owner) and self.Owner:GetViewModel()) then
				self:IdleAnimation(self.Owner:GetViewModel():SequenceDuration())
			end
		end)
	end

	self:SecondThink()

	if self.IdleDelay < CurTime() and self.IdleApply and self.Weapon:Clip1() > 0 then
		local WeaponModel = self.Weapon:GetOwner():GetActiveWeapon():GetClass()

		if self.Weapon:GetOwner():GetActiveWeapon():GetClass() == WeaponModel and self.Owner:Alive() then
			self.Weapon:SendWeaponAnim(ACT_VM_IDLE)

			if self.AllowPlaybackRate and not self.Weapon:GetDTBool(1) then
				self.Owner:GetViewModel():SetPlaybackRate(1)
			else
				self.Owner:GetViewModel():SetPlaybackRate(0)
			end		
		end

		self.IdleApply = false
	elseif self.Weapon:Clip1() <= 0 then
		self.IdleApply = false
	end

	self:NextThink(CurTime())
end


/*---------------------------------------------------------
	PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack(secondary)

	self.Weapon:SetNextSecondaryFire(CurTime() + self.Primary.Delay)
	if self.Owner:GetNWBool("Double Tapd") then
		self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay * 0.675)
	else
		self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
	end
	
	if (!self:CanPrimaryAttack()) then return end

	self.Weapon:EmitSound(self.Primary.Sound)
	if (SERVER) then
		self:FireGrenade(secondary)
	end
	local effectdata = EffectData()
		effectdata:SetOrigin(self.Owner:GetShootPos())
		effectdata:SetEntity(self.Weapon)
		effectdata:SetStart(self.Owner:GetShootPos())
		effectdata:SetNormal(self.Owner:GetAimVector())
		effectdata:SetAttachment(1)
	util.Effect("effect_mad_shotgunsmoke", effectdata)

	self.Weapon:SetClip1(self.Weapon:Clip1() - 1)
	if (self.Owner:IsNPC()) then return end
	
	self.Owner:ViewPunch(Angle(math.Rand(-0.4,0.4) * self.Primary.Recoil, math.Rand(-0.4,0.4) *self.Primary.Recoil, 0))
	if ((SinglePlayer() && SERVER) || CLIENT) then
		self.Weapon:SetNetworkedFloat("LastShootTime", CurTime())
	end
	
end

/*---------------------------------------------------------
	SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
	if (self:GetUpgrade() > 0) then
		self:PrimaryAttack(true)	
	end
end

/*---------------------------------------------------------
   Name: SWEP:PrimaryAttack()
   Desc: +attack1 has been pressed
---------------------------------------------------------*/
function SWEP:FireGrenade(secondary)

	local object = ents.Create("shot_glround")
	// grenade_ar2
	if ValidEntity(object) then	
		local pos = self.Owner:GetShootPos()
			pos = pos + self.Owner:GetForward() * 5
			pos = pos + self.Owner:GetRight() * 9
			pos = pos + self.Owner:GetUp() * -7
		object:SetPos(pos)	
		object:SetNWEntity("owner", self.Owner)
		object:SetAngles(self.Owner:EyeAngles())
		if (secondary == true) then
		object.TouchExplode = true
		end
		object:Spawn()
		if (self:GetUpgrade() > 0) then
			object:GetPhysicsObject():SetVelocity(self.Owner:GetAimVector()*3000)
		else
			object:GetPhysicsObject():SetVelocity(self.Owner:GetAimVector()*2200)
		end
	end
	
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK) 		// View model animation
	self.Owner:MuzzleFlash()								// Crappy muzzle light
	self.Owner:SetAnimation(PLAYER_ATTACK1)				// 3rd Person Animation
	
	if (self.Owner:IsNPC()) then return end
end

/*---------------------------------------------------------
	Checks the objects before any action is taken
	This is to make sure that the entities haven't been removed
---------------------------------------------------------*/
function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)	
	draw.SimpleText(self.IconLetter, self.IconFont, x + wide/2, (y + tall*0.2)-10, Color(255, 210, 0, 255), TEXT_ALIGN_CENTER)
	if (self:GetUpgrade() > 0) then
		draw.SimpleText("K", "CSKillIcons", x + wide-20, y + tall-25, Color(200,200, 200, 255), TEXT_ALIGN_CENTER)
	end
end

function SWEP:Upgrade()
	self.Upgradestring = "Secondary fire mode explodes on contact"
end